package com.gukun.springboot.tdengine.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gukun.springboot.tdengine.domain.common.CommonResult;
import com.gukun.springboot.tdengine.domain.common.GlobalErrorCodeConstants;
import com.gukun.springboot.tdengine.domain.managetag.DbsManagetagInfo;
import com.gukun.springboot.tdengine.dto.ExportTableDto;
import com.gukun.springboot.tdengine.dto.ShowTableDto;
import com.gukun.springboot.tdengine.pojo.DBConfig;
import com.gukun.springboot.tdengine.service.managerpoints.ManagerPointsInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@RequestMapping("/api/tdengine/managerpoints/")
@RestController
public class ManagerPointsController {
    @Autowired
    private ManagerPointsInfoService managerPointsInfoService;
    /**
     * 测点表导入或者删除
     *type true 导入 false 删除
     * @return 导入结果
     */
    @Operation(summary = "测点表导入或者删除", description = "测点表导入或者删除")
    @PostMapping("/uploadFile")
    public CommonResult<String> uploadFile(@RequestBody MultipartFile file, String stable,String host,String port,String username,
                                           String password,String dbName,boolean type) throws Exception{
        DBConfig dbConfig = managerPointsInfoService.checkFileAndDbConfigInfo(file,username,password,host,dbName,port);
        return managerPointsInfoService.uploadFile(file,stable,dbConfig,type);
    }

    /**
     * 通过文件修改测点描述
     * @return 修改结果
     */
    @Operation(summary = "通过文件修改测点描述", description = "通过文件修改测点描述")
    @PostMapping("/updateTagDescByFile")
    public CommonResult<Boolean> updateTagDescByFile(@RequestBody MultipartFile file, String host,String port,String username,
                                           String password,String dbName) throws Exception{
        DBConfig dbConfig = managerPointsInfoService.checkFileAndDbConfigInfo(file,username,password,host,dbName,port);
        return CommonResult.success(managerPointsInfoService.updateTagDescByFile(file,dbConfig));
    }

    /**
     * 通过文件修改测点tn
     * @return 修改结果
     */
    @Operation(summary = "通过文件修改测点tn", description = "通过文件修改测点tn")
    @PostMapping("/updateTagTnByFile")
    public CommonResult<Boolean> updateTagTnByFile(@RequestBody MultipartFile file, String host,String port,String username,
                                                     String password,String dbName) throws Exception{
        DBConfig dbConfig = managerPointsInfoService.checkFileAndDbConfigInfo(file,username,password,host,dbName,port);
        return CommonResult.success(managerPointsInfoService.updateTagTnByFile(file,dbConfig));
    }

    /**
     * 查询测点表信息
     *
     * @return 测点信息
     */
    @Operation(summary = "查询测点表信息", description = "查询测点表信息")
    @PostMapping("/showTables")
    public CommonResult<Page<DbsManagetagInfo>> showTables(@RequestBody ShowTableDto showTableDto) throws SQLException, IOException {
        Page<DbsManagetagInfo> page = managerPointsInfoService.showTables(showTableDto);
        if(page== null){
            return CommonResult.error(500,"查询出错，请联系管理员");
        }
        return CommonResult.success(page);
    }

    /**
     * 删除测点表信息
     *
     * @return 删除信息
     */
    @Operation(summary = "删除测点表信息", description = "删除测点表信息")
    @PostMapping("/deleteTables")
    public CommonResult<String> deleteTables(@RequestBody List<DbsManagetagInfo> params){
        String result = managerPointsInfoService.deleteTables(params);
        if(result== null){
            return CommonResult.error(500,"删除出错，请联系管理员");
        }
        return CommonResult.success("删除成功");
    }

    /**
     * excel文件的导出
     *
     * @return 二进制文件流
     */
    @Operation(summary = "文件的导出", description = "文件的导出")
    @PostMapping("/export")
    public void export(@RequestBody ExportTableDto exportTableDto) throws IOException, SQLException {
        HttpServletResponse response = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getResponse();
        HttpServletRequest request =  ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();

        Workbook workbook = managerPointsInfoService.getExport(exportTableDto);
        // 设置响应头
        response.setHeader("Content-Disposition", "attachment; filename=\"data.xlsx\"");
        response.setStatus(HttpServletResponse.SC_OK);

        // 写入响应流
        workbook.write(response.getOutputStream());
        workbook.close();
    }

    /**
     * 同步信息
     *
     * @return 同步结果
     */
    @Operation(summary = "同步信息", description = "同步信息")
    @PostMapping("/syncDBToTDengine")
    public CommonResult<String> syncDBToTDengine(@RequestBody ShowTableDto showTableDto) throws SQLException {
        managerPointsInfoService.syncDBToTDengine(showTableDto);
        return CommonResult.success("同步成功");
    }




}
